import java.util.StringTokenizer;
import java.lang.reflect.Array;

public class Branch
{
	public static void convert(String line, int lineNumber, Assembled assembled)
	{
		String temp = oldconvert(line, assembled);
		StringTokenizer tokenizer = new StringTokenizer(temp);
		int[] build = new int[tokenizer.countTokens() + 2];
		int i = 1;
		
		build[0] = 2;//check this value;
		while(tokenizer.hasMoreTokens())
			build[i++] = Integer.parseInt(tokenizer.nextToken());
                build[i] = lineNumber;
                
                assembled.addLine(build);
	}
	
	//creates the output used by a branching command
	public static String oldconvert(String value, Assembled assembled)
	{
		StringTokenizer tokenizer = new StringTokenizer(value, " ,\t");
		String output = new String("");
		String temp = tokenizer.nextToken();
		String firstValue = jtable(temp);
		String secondValue = tokenizer.nextToken();
		int location;
		
		temp = temp.trim();

		if(temp.equals("jr"))//jr	$ra		# return 
			output = firstValue + " " + Memory.memory(secondValue) + " " + "8"; //"00000000000000001000";
			
		else if((temp.equals("j")) || (temp.equals("jal")))//j	endif //jal towers	# recursive call to towers		# jump past else 
		{
			location = assembled.getLabelAddress(secondValue);
			
			output = firstValue + " " + location;
		}
		
		else if(temp.equals("bge"))//bge	$a0,	2,	else # numDiscs < 2
		{
			output = firstValue + " " + Memory.memory(secondValue) + " " + tokenizer.nextToken() + " " + assembled.getLabelAddress(tokenizer.nextToken());
		}
		
		else
		{
			output = firstValue + " " + Memory.memory(secondValue) + " " + Memory.memory(tokenizer.nextToken());
			
			location = assembled.getLabelAddress(tokenizer.nextToken());
			
			output = output + " " + location;
		}
		
		return output;
	}
	
	//a table of the branch command values used by the simulator
	public static String jtable(String command)
	{
		if(command.equals("j"))
			return "2"; //"000010";
		if(command.equals("jr"))
			return "1"; //"000000";
		if(command.equals("jal"))
			return "3"; //"000011";
		if(command.equals("bne"))
			return "5"; //"000101";
		if(command.equals("beq"))
			return "4"; //"000100";
		if(command.equals("bge"))
			return "6";
		
		return "";
	}
}